<?php
class ControllerUser { 

    function getFunction($op){
        
        switch ($op) {
            case "new":
                $this->newUser();
                break;
            
            case "list":
                $this->listUser();
                break;
            
            case "save":
                $this->saveUser();
                break;

            case "edit":
                $this->editUser();
                break;
            
            case "update":
                $this->updateUser();
                break;
            
            case "del":
                $this->deleteUser();
                break;
            
            case "combo":
                return $this->comboUser();
                break;
            
            default:
                echo "ERROR NO HAY CONTROLADOR PARA ESA OPCION";
                break;
        }
    }
    

    function newUser(){
        include_once "vista/vista.user.php";
        form_user();
    }    
    

    function saveUser(){
        include_once "modelo/modelo.user.php";
        
        $u = new user();
        $u->set_user_id($_REQUEST["user_id"]); 
        $u->set_user_fb_id($_REQUEST["user_fb_id"]); 
        $u->set_user_name($_REQUEST["user_name"]); 
        $u->set_user_first_name($_REQUEST["user_first_name"]); 
        $u->set_user_last_name($_REQUEST["user_last_name"]); 
        $u->set_user_link($_REQUEST["user_link"]); 
        $u->set_user_username($_REQUEST["user_username"]); 
        $u->set_user_g_id($_REQUEST["user_g_id"]); 
        $u->set_user_ut_id($_REQUEST["user_ut_id"]); 
        $u->set_user_status($_REQUEST["user_status"]); 

        
        if($u->Save()){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_ok']." '> <img src='".$_SESSION['alert']['image_ok']."' /> ".$_SESSION['alert']['msj_save_ok']." </div>";
        }else{
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_save_error']." </div>";
        }
        $this->listUser($msj);
        
    }    
    
    function editUser(){
        include_once 'modelo/modelo.user.php';
        include_once 'vista/vista.user.php';
        
        $u = new user();
        $u->setuser_id($_REQUEST["user_id"]); 
        
        $u->getData();
        
        form_user($u);
        
    }    
    
    function deleteUser(){
        include_once 'modelo/modelo.user.php';
        
        $u = new user();
        $u->setuser_id($_REQUEST["user_id"]); 
        
        if($u->Delete()){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_ok']." '> <img src='".$_SESSION['alert']['image_ok']."' /> ".$_SESSION['alert']['msj_delete_ok']." </div>";
        }else{
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_delete_error']." </div>";
        }
        $this->listUser($msj);
    }

    
    function updateUser(){
        include_once 'modelo/modelo.user.php';
        
        $u = new user();
        $u->set_user_id($_REQUEST["user_id"]); 
        $u->set_user_fb_id($_REQUEST["user_fb_id"]); 
        $u->set_user_name($_REQUEST["user_name"]); 
        $u->set_user_first_name($_REQUEST["user_first_name"]); 
        $u->set_user_last_name($_REQUEST["user_last_name"]); 
        $u->set_user_link($_REQUEST["user_link"]); 
        $u->set_user_username($_REQUEST["user_username"]); 
        $u->set_user_g_id($_REQUEST["user_g_id"]); 
        $u->set_user_ut_id($_REQUEST["user_ut_id"]); 
        $u->set_user_status($_REQUEST["user_status"]); 

        if($u->Update()){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_ok']." '> <img src='".$_SESSION['alert']['image_ok']."' /> ".$_SESSION['alert']['msj_update_ok']." </div>";
        }else{
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_update_error']." </div>";
        }
        $this->listUser($msj);
    }

    
    function listUser($msj=false){
        include_once 'modelo/modelo.user.php';
        include_once 'vista/vista.user.php';
        
        $u = new user();

        $u->setuser_id($_REQUEST["user_id"]); 
        $u->getData();
        
        $dataTotal = $u->GetList(" count(*) ", false,  true);
        $dataTotal = $dataTotal[0][0]; 
        $data = $u->GetList(" * ", false);
        if(is_null($data)){
            $msj = "<div id='alertzone' style='".$_SESSION['alert']['style_error']."'> <img src='".$_SESSION['alert']['image_error']."' /> ".$_SESSION['alert']['msj_empty_error']." </div>";
        }
        list_user($data, $msj, $dataTotal);

    }

}